﻿namespace IBTrader.Indicator.Channel
{
    using IBTrader.Indicator.Average;
    using IBTrader.Indicator.CandleStick;
    using IBTrader.Modules.Read;
    using IBTrader.Charts;
    using System;

    class KeltnerChannel : Base, IUpDown
    {
        public event EventHandler<Line> Up, Down;
        private readonly ATR atr;
        private readonly EMA ema;
        private Line emaLine = null;
        public KeltnerChannel(Worker worker, int interval = 10, int einterval = 20, int pinterval = 60, double factor = 1.0)
            : base(worker, pinterval)
        {
            base.chartArgs.Add(ChartArgs.EventType.Up, new ChartArgs { Group = 1 });
            base.chartArgs.Add(ChartArgs.EventType.Down, new ChartArgs { Group = 1 });
            ema = new EMA(worker, einterval);
            atr = new ATR(worker, interval);
            ema.Hit += (_, line) => emaLine = line;
            atr.Hit += (_, line) =>
            {
                OnHit(Up, ChartArgs.EventType.Up, line, emaLine.Price + factor * line.Price);
                OnHit(Down, ChartArgs.EventType.Down, line, emaLine.Price - factor * line.Price);
                OnHit(line, emaLine.Price);
            };
        }
        protected override void Add(object sender, Item item)
        {
            atr.Add(sender, item);
        }
        public override void Add(object sender, Line line)
        {
            base.Add(sender, line);
            ema.Add(sender, line);
        }
    }
}